#include "Generate.h"
#import "RSReplay.h"
#import <AppKit/AppKit.h>

OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
{
	NSURL* nsUrl = (NSURL*)url;
	if (!nsUrl.isFileURL)
		return -1;
	
	CGImageRef previewImage = [RSReplay previewOfFile:nsUrl.path];
	CGSize previewSize = CGSizeMake(CGImageGetWidth(previewImage), CGImageGetHeight(previewImage));
	
	CGContextRef ctx = QLThumbnailRequestCreateContext(thumbnail, previewSize, YES, NULL);
	CGContextDrawImage(ctx, CGRectMake(0, 0, previewSize.width, previewSize.height), previewImage);
	QLThumbnailRequestFlushContext(thumbnail, ctx);
	CGContextRelease(ctx);

    return noErr;
}

void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
{
    // not implemented
}
